//
//  QYGSetTableViewController.m
//  wawaji_ios
//
//  Created by yiguo qi on 2017/11/30.
//  Copyright © 2017年 yiguo qi. All rights reserved.
//

#import "QYGSetTableViewController.h"
#import "QYGMineTableViewCell.h"
#import "QYGOnlineServiceViewController.h"
#import "QYGNetRequestModel.h"
#import "QYGLoginViewController.h"

static NSString *settingCell = @"settingCell";
static NSString *logOutCell = @"logOutCell";

@interface QYGSetTableViewController ()

@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, strong) UIView *loginOutView;
@property (nonatomic, strong) UILabel *loginOutLabel;

@end

@implementation QYGSetTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"设置";
    
    [self setupData];
    [self setupUI];
    
}

- (void)setupUI{
    
    [self.tableView registerClass:[QYGMineTableViewCell class] forCellReuseIdentifier:settingCell];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:logOutCell];
    self.tableView.separatorColor = [UIColor clearColor];
//    self.tableView.tableFooterView = [UIView new];
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    
    self.tableView.backgroundColor = [UIColor cm_backgroudColor];
    self.tableView.scrollEnabled = NO;
    
//    [self.view addSubview:self.loginOutView];
    
}
- (void)setupData{
    
    self.dataArray = @[
//                       @[@{@"title" : @"背景音乐",
//                           @"icon"  : @"mine_bgMusic"
//                           },
//                          @{@"title" : @"问题反馈",
//                            @"icon"  : @"mine_fankui"
//                            },
//                          @{@"title" : @"关于我们",
//                            @"icon"  : @"mine_about"
//                            }
//                         ]
                       @[@{}]
                       ];
    
}
- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.loginOutView.x = 0;
    
    self.loginOutView.y = self.view.height - self.loginOutView.height -SafeBottom;
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSArray *array = self.dataArray[section];
    return array.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return ScaleH(13);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ScaleH(55);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if(indexPath.section == self.dataArray.count - 1){
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:logOutCell forIndexPath:indexPath];
        [cell addSubview:self.loginOutLabel];
        self.loginOutLabel.frame = cell.bounds;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        NSDictionary *dic  = self.dataArray[indexPath.section][indexPath.row];
        QYGMineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:settingCell forIndexPath:indexPath];
        cell.indexPath = indexPath;
        cell.iconStr = dic[@"icon"];
        cell.nameStr = dic[@"title"];
    //    if(indexPath.section == 0){
            cell.mineAccessoryType = QYGMineTableViewCellAccessoryDetailSwitch;
            if(indexPath.row == 0){
                cell.isOn = [QYGKeychain bgMusicIsClose];
            }else if(indexPath.row == 1){
                cell.isOn = [QYGKeychain soundType];
            }
    //    }else{
            cell.mineAccessoryType = QYGMineTableViewCellAccessoryDisclosureIndicator;
    //    }
        
        [self dealWithSwChangeWithCell:cell];
        
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(indexPath.section == self.dataArray.count - 1){
        [self loginOutBtnClick];
    }
//    if(indexPath.section == 1){
//        if(indexPath.row == 0){
//            QYGOnlineServiceViewController *onlineServiceVC = [[QYGOnlineServiceViewController alloc] init];
//            [self.navigationController pushViewController:onlineServiceVC animated:YES];
//        }
//    }
}

#pragma mark target
- (void)loginOutBtnClick{
//    return;
    [QYGNetRequestModel getLoginOutCompleted:^(BOOL isSuccess) {
        if(!isSuccess)
        {
             [self.view makeToast:@"退出失败，请重试！"];
            return ;
        }else{
            [QYGKeychain deleteToken];
            KeyWindow.rootViewController =  [[QYGLoginViewController alloc] init];
            [KeyWindow makeToast:@"退出登录成功！"];
        }
        
    } WithErrorCodeBlock:^(NSError *error) {
    }];
}

#pragma mark - 内部方法
- (void)dealWithSwChangeWithCell:(QYGMineTableViewCell *)cell
{
    [cell setSwChangeBlock:^(QYGMineTableViewCell *mineCell, BOOL isOn) {
        QYLog(@"%@", isOn? @"yes" : @"no");
        NSIndexPath *indexPath = mineCell.indexPath;
        if(indexPath.section == 0)
        {
            if(indexPath.row == 0){
                // 更新背景音乐状态
                QYLog(@"更新背景音乐状态");
                
                [QYGKeychain setBgMusicIsClose:isOn];
            }else{
                // 更新音效状态
                QYLog(@"更新音效状态");
                [QYGKeychain setSoundType:isOn];
                
            }
        }
    }];
}
#pragma mark lazy

- (UIView *)loginOutView{
    if(!_loginOutView){
        _loginOutView = [SGUtil newViewWithBgColor:[UIColor whiteColor]];
        _loginOutView.size = CGSizeMake(SCREEN_WIDTH, ScaleW(65));
        UIButton *loginOutBtn = [UIButton buttonWithTitle:@"退出登录" setTitleFont:ScaleBoldFont(16)];
        loginOutBtn.backgroundColor = [UIColor cm_mainColor];
        CGFloat w = ScaleW(280);
        CGFloat h = ScaleW(40);
        CGFloat x = (_loginOutView.width - w) * 0.5;
        CGFloat y = (_loginOutView.height - h) * 0.7;
        loginOutBtn.frame = CGRectMake(x, y, w, h);
        [loginOutBtn addTarget:self action:@selector(loginOutBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [loginOutBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        loginOutBtn.layer.cornerRadius = 4;
        [_loginOutView addSubview:loginOutBtn];
   
    }
    return _loginOutView;
    
}

- (UILabel *)loginOutLabel{
    if(!_loginOutLabel){
        _loginOutLabel = [UILabel labelWithText:@"退出登录" setFont:ScaleFont(16) setTextColor:[UIColor colorWithHex:@"333333"] setTextAlignment:NSTextAlignmentCenter];
        [_loginOutLabel sizeToFit];
    }
    return _loginOutLabel;
}
@end
